*************************************************************************
****************** GENERATE PRIMARY DATA FOR ANALYSIS *******************
*************************************************************************

cd "D:\Replication"

use incdistribution.dta, clear
merge 1:1 commid WAVE using so2_match_long.dta, keep(match) nogen

* propensity score matching
global score comm denc div econ health house market soc trans edc mart sani 
keep commid WAVE $score so2* tcz gini

* pre-policy periods
keep if WAVE<1998

collapse (mean) $score so2* gini, by(commid tcz)
psmatch2 tcz $score, out(gini so2_power_ann) kernel common bwidth(0.01)

psgraph

* before$score
twoway (kdensity _pscore if _treated==1) (kdensity _pscore if _treated==0, ///
lpattern(dash)), legend( label( 1 "treated") label( 2 "control" ) ) ///
xtitle("propensity scores BEFORE matching") name(before, replace) plotregion(color(white)) graphregion(color(white))

* after
twoway (kdensity _pscore if _treated==1 [aweight=_weight]) (kdensity _pscore if _treated==0 [aweight=_weight], lpattern(dash)),  ///
legend( label( 1 "treated") label( 2 "control" )) ///
xtitle("propensity scores AFTER matching") name(after, replace) plotregion(color(white)) graphregion(color(white))

* combined
grc1leg before after, ycommon plotregion(color(white)) graphregion(color(white))
 
* keep sample with common support
gen common=_support
drop if common == 0 
drop if common==.

keep commid _pscore _treated _support _weight 
save kernelmatch.dta, replace

* construct data for analysis

use incdistribution.dta, clear
merge 1:1 commid WAVE using so2_match_long.dta, keep(match) nogen 
merge m:1 commid using kernelmatch.dta, keep(match) nogen

local varname gini theil pctratio_p90p10 pctratio_p75p25 
foreach var of local varname{
g log`var'=log(`var')	
}

g post=WAVE>1998
g dd=tcz*post
label variable dd "Treat X Post"

save analysis_main.dta, replace

